<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container" @mouseleave="leave" @mouseenter="show = true">
      <h2 class="all">全部商品分类</h2>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
      <transition name="sort">
        <div class="sort" v-show="show">
          <div class="all-sort-list2" @click="goSearch">
            <div
              class="item"
              v-for="(item1, index) in categoryList"
              :key="item1.categoryId"
              @mouseenter="changeIndex(index)"
              :class="{ current: index === currentIndex }"
            >
              <h3>
                <a
                  :data-categoryName="item1.categoryName"
                  :data-category1Id="item1.categoryId"
                  href="#"
                  >{{ item1.categoryName }}</a
                >
                <!-- 消耗性能 -->
                <!-- <router-link to="/search">{{ item1.categoryName }}</router-link> -->
              </h3>
              <div
                class="item-list clearfix"
                :style="{ display: currentIndex === index ? 'block' : 'none' }"
              >
                <div
                  class="subitem"
                  v-for="item2 in item1.categoryChild"
                  :key="item2.categoryId"
                >
                  <dl class="fore">
                    <dt>
                      <a
                        :data-categoryName="item2.categoryName"
                        :data-category2Id="item2.categoryId"
                        href="#"
                        >{{ item2.categoryName }}</a
                      >
                    </dt>
                    <dd>
                      <em
                        v-for="item3 in item2.categoryChild"
                        :key="item3.categoryId"
                      >
                        <a
                          href="#"
                          :data-categoryName="item3.categoryName"
                          :data-category3Id="item3.categoryId"
                          >{{ item3.categoryName }}</a
                        >
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import throttle from 'lodash/throttle'
export default {
  data () {
    return {
      // 保存鼠标所在的分类的index
      currentIndex: null,
      show: this.$route.path == '/home' ? true : false
    }
  },
  methods: {
    // changeIndex (index) {
    // 正常情况(用户慢慢移动) ->进入每一个h3都会触发鼠标进入事件
    // 非正常情况(用户快速移动) -> 只有部分h3会触发鼠标进入事件
    // 原因是由于用户行为过快,导致浏览器反应不过来,如果当前回调函数中有大量业务,则会发生卡顿现象,所以要引入函数防抖与节流
    // 节流 在规定的间隔范围内不会重复触发回调,只有大于这个时间间隔才会触发回调,把频繁触发变为少量触发
    // 防抖 前面的所有触发都被取消,最后一次执行在规定的时间之后才会触发,也就是说如果连续快速的触发,只会触发一次
    // this.currentIndex = index
    // console.log(this.currentIndex)
    // }
    changeIndex: throttle(function (index) {
      this.currentIndex = index
    }, 50),
    // 路由跳转的方法
    goSearch (event) {
      // console.log(event.target)
      // console.log(event.target.dataset)
      // 注意dataset中的自定义属性是全小写,而不是我们定义的驼峰写法
      let { categoryname, category1id, category2id, category3id } = event.target.dataset
      if (categoryname) {
        let location = { name: "search" }
        let query = { categoryName: categoryname }
        if (category1id) {
          query.category1Id = category1id
        } else if (category2id) {
          this.$router.push(location, query)
        } else if (category3id) {
          query.category3Id = category3id
        }
        //如果路由跳转的时候带有params参数,也要一起携带
        if (this.$route.params) {
          location.params = this.$route.params
        }
        location.query = query
        // console.log(location)
        this.$router.push(location)
      }
    },
    leave () {
      if (this.$route.path != '/home') {
        this.show = false
      }
      this.currentIndex = null
    }
  },
  created () {
    // this.$store.dispatch('home/getCategoryList')
  },
  computed: {
    ...mapState('home', ['categoryList'])
  },
}
</script>

<style lang="less" scoped>
.current {
  background-color: skyblue;
}
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &:hover {
            .item-list {
              display: block;
            }
          }
        }
      }
    }
    // 过渡动画的样式
    // 过渡动画开始状态
    .sort-enter {
      height: 0px;
    }
    .sort-enter-to {
      height: 461px;
    }
    // 定义动画时间 速率
    .sort-enter-active {
      transition: all 0.5s linear;
    }
  }
}
</style>